const { Code2session } = require("./api.js");
const { getRequest } = require("request");
const { util, InsideErr } = require("utils");
async function wxMiniLogin(appId, appSecret, code) {
    let [err, data] = await getRequest(Code2session)({
        appid: appId,
        secret: appSecret,
        grant_type: "authorization_code",
        js_code: code,
    });
    /*
    {
        "session_key": "wE6FPacKHyNjWCOvfjO1Qw==",
        "openid": "o6O8x5IkJb1jOm5y572eIcmKjR7Y",
        "unionid": "omYVS6kKAq6hcvdNbPFZHg1OyLSI"
    }
    */
    if (!util.nilValue(err)) {
        return [err, null];
    }
    let resData = data || {};
    if (!util.nilValue(resData.errcode) && resData.errcode !== 0) {
        let err = new InsideErr(resData);
        return [err, null];
    } else {
        return [null, resData];
    }
}

module.exports = {
    wxMiniLogin,
};
